<?php
require('conf_image.php')
$uri = $_GET['uri'];

//localhost:8000/9gag/download/150x150/2013/06/04/47/e26ba7e9d82c2f106e1e604cb55e32d0.jpg
if(preg_match('/(\d+x\d+)\/(.+)\/(.+)\.(.+)$/is', $uri, $match)){
	#echo 'Looking for thumbnail size "'.$match[1].'" in folder '.$match[2].'<br>';
	if(!array_key_exists($match[1], $arrThumbSize)) die('File not found');
	$arrDim = explode('x', $match[1]);
	$thumbFolder = $rootPath.$match[1].'/'.$match[2];
	$originFolder = $rootPath.$match[2];
	$fileName = $match[3].'.'.$match[4];
	//$thumbFile = $match[2].'-'.$match[1].'.'.$match[4];
	if(file_exists($thumbFolder.'/'.$fileName)){
		header('HTTP/1.1 301 Moved Permanently');
		header('Location: '.$rootUrl.$match[1].'/'.$match[2].'/'.$fileName);
		die();
	}
	elseif(file_exists($originFolder.'/'.$fileName)){
		if(!is_dir($thumbFolder)){
			@mkdir($thumbFolder, 0777, true);
		}
		
		// *** Include the class
		include("resize-class.php");
		// *** 1) Initialise / load image
		$resizeObj = new resize($originFolder.'/'.$fileName);
		// *** 2) Resize image (options: exact, portrait, landscape, auto, crop)
		$resizeObj -> resizeImage($arrDim[0], $arrDim[1], $arrThumbSize[$match[1]]);
		// *** 3) Save image
		$resizeObj -> saveImage($thumbFolder.'/'.$fileName, 100);
		header('Location: '.$rootUrl.$match[1].'/'.$match[2].'/'.$fileName);
		die();
	}
	else{
		die('File not found');
	}
}
die();

?>
